package com.localization;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
public class FxStringUtil {


    /**
     * 字节码转字符串
     * 
     * @param b
     * @return
     */
    public static String byte2hex(byte[] b) {
        // 一个字节的数，转成16进制字符串
        String hs = "";
        String tmp = "";
        for (int n = 0; n < b.length; n++) {
            // 整数转成十六进制表示
            tmp = (Integer.toHexString(b[n] & 0XFF));
            if (tmp.length() == 1) {
                hs = hs + "0" + tmp;
            } else {
                hs = hs + tmp;
            }
        }
        tmp = null;
        return hs.toUpperCase();
    }

    /**
     * 字符串转java字节码
     * 
     * @param b
     * @return
     */
    public static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0) {
            throw new IllegalArgumentException("长度不是偶数");
        }
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个进制字节
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        b = null;
        return b2;
    }

    /**
     * 将10进制数字转为16进制字符串并补足4位
     * 
     * @param i
     * @return
     */
    public static String to16Str(Integer i) {
        String s = Integer.toHexString(i).toUpperCase();
        if (s.length() == 1) {
            s = "000" + s;
        } else if (s.length() == 2) {
            s = "00" + s;
        } else if (s.length() == 3) {
            s = "0" + s;
        }
        return s;
    }

    /**
     * 将10进制数字转为16进制字符串并补足2位
     * 
     * @param i
     * @return
     */
    public static String to16Str2(String i) {
        String s = Integer.toHexString(Integer.parseInt(i)).toUpperCase();
        if (s.length() == 1) {
            s = "0" + s;
        }
        return s;
    }

    /**
     * 将16进制字符串转为10进制字符串
     * 
     * @parami
     * @return
     */
    public static String to10Str(String s) {
        s = Integer.parseInt(s, 16) + "";
        return s;
    }

    /**
     * 将16进制字符串转为10进制字符串并补足2位
     * 
     * @parami
     * @return
     */
    public static String to10Str2(String s) {
        s = Integer.parseInt(s, 16) + "";
        if (s.length() == 1) {
            s = "0" + s;
        }
        return s;
    }

    /**
     * 将10进制字符串进行16进制异或计算并补足2位
     * 
     * @parami
     * @return
     */
    public static String xorFor16Str(String str) {
        Integer result = 0;
        for (int i = 0; i < str.length(); i = i + 2) {
            // 十六进制转成十进制
            if (i == 0) {
                result = Integer.valueOf(str.substring(i, i + 2), 16);
            } else {
                result = result ^ Integer.valueOf(str.substring(i, i + 2), 16);
            }
        }
        String resultStr = Integer.toHexString(result).toUpperCase();
        // 不足两位前面补0
        if (resultStr.length() == 1) {
            resultStr = "0" + resultStr;
        }
        return resultStr;
    }

    /**
     * 获取本机MAC地址
     * 
     * @param ia
     * @throws SocketException
     */
    public static String getLocalMac(InetAddress ia) throws SocketException {
        // 获取网卡，获取地址
        byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
        //TODO 在虚拟机下获取不到mac，暂时用00
        if(mac==null){
            return "000000000000";
        }
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < mac.length; i++) {
            // 字节转换为整数
            int temp = mac[i] & 0xff;
            String str = Integer.toHexString(temp);
            if (str.length() == 1) {
                sb.append("0" + str);
            } else {
                sb.append(str);
            }
        }
        return sb.toString().toUpperCase();
    }


    public static void main(String[] args) throws Exception {
        // String str = createAuthId(84l);
        // System.out.println(str);
        // System.out.println(to10Str("000001DB"));
         InetAddress ia = InetAddress.getLocalHost();
         System.out.println("本机MAC地址:" + getLocalMac(ia));
    }

}
